home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / Effect library / Demo ƒ / Reversed fades ƒ / Hilbert fade reversed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-12  |  4.2 KB  |  147 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        Hilbert fade reversed.c
  4.  
  5. Purpose:    Graphic effect to fade main window to solid pattern.
  6.             See comments below for more description.
  7.  
  8.  
  9. MSG Demo -- graphic effects demonstration program
  10. Copyright (C) 1992-4 Mark Pilgrim & Dave Blumenthal
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program in a file named "GNU General Public License".
  24. If not, write to the Free Software Foundation, 675 Mass Ave,
  25. Cambridge, MA 02139, USA.
  26.  
  27. \**********************************************************************/
  28.  
  29. #include "msg timing.h"
  30.  
  31. /* This fills the screen by a Hilbert space-filling curve, which is defined by
  32.    two mutually recursive drawing functions:
  33.    
  34.    X = left, Y, draw, right, X, draw, X, right, draw, Y, left
  35.    Y = right, X, draw, left, Y, draw, Y, left, draw, X, right
  36.    
  37.    Start by drawing X at the highest recursion level from the bottomleft of the
  38.    screen. (At recursion level 1, X and Y are null functions.)
  39.    
  40.    [Note that the procedure below actually draws the Hilbert curve flipped
  41.    horizontally on the screen, so it starts drawing at the bottomright and
  42.    works leftward.]
  43. */
  44.  
  45. #define    RecursionLevel    4
  46. #define CorrectTime 1
  47. #define theWindowHeight (boundsRect.bottom-boundsRect.top)
  48. #define theWindowWidth (boundsRect.right-boundsRect.left)
  49.  
  50. pascal void HilbertFadeRecurseReversed(Rect boundsRect, Pattern *thePattern,
  51.     int level, int whichpattern, int* x, int* y);
  52. pascal short HilbertFadeReversed(Rect boundsRect, Pattern *thePattern);
  53.  
  54. typedef char    Hilby[11];
  55.  
  56. static Hilby    HilbertPattern[]=
  57. {
  58.     {
  59.         0x02,0x69,0x00,0x01,0x42,0x00,0x42,0x01,0x00,0x69,0x02
  60.     },
  61.     {
  62.         0x01,0x42,0x00,0x02,0x69,0x00,0x69,0x02,0x00,0x42,0x01
  63.     }
  64. };
  65.  
  66. static int            HilbertDirection;
  67. static int            vBlockSize;
  68. static int            hBlockSize;
  69. static RgnHandle    boundsRgn;
  70.  
  71. pascal void HilbertFadeRecurseReversed(Rect boundsRect, Pattern *thePattern,
  72.     int level, int whichpattern, int* x, int* y)
  73. {
  74.     int                i;
  75.     Rect            source;
  76.     
  77.     for (i=0; i<11; i++)
  78.     {
  79.         switch (HilbertPattern[whichpattern][i])
  80.         {
  81.             case 0x01:     /* turn left */
  82.                 HilbertDirection--;
  83.                 if (HilbertDirection<0) HilbertDirection=3;
  84.                 break;
  85.             case 0x02:     /* turn right */
  86.                 HilbertDirection++;
  87.                 if (HilbertDirection==4) HilbertDirection=0;
  88.                 break;
  89.             case 0x00:    /* draw */
  90.                 StartTiming();
  91.                 SetRect(&source, theWindowWidth-*x-hBlockSize, *y-vBlockSize,
  92.                     theWindowWidth-*x, *y);
  93.                 OffsetRect(&source, boundsRect.left, boundsRect.top);
  94.                 SectRect(&source, &boundsRect, &source);
  95.                 FillRect(&source, *thePattern);
  96.                 switch (HilbertDirection)
  97.                 {
  98.                     case 0:
  99.                         *x+=hBlockSize;
  100.                         break;
  101.                     case 1:
  102.                         *y-=vBlockSize;
  103.                         break;
  104.                     case 2:
  105.                         *x-=hBlockSize;
  106.                         break;
  107.                     case 3:
  108.                         *y+=vBlockSize;
  109.                         break;
  110.                 }
  111.                 TimeCorrection(CorrectTime);
  112.                 break;
  113.             case 0x42:    /* call X */
  114.                 if (level>1)
  115.                     HilbertFadeRecurseReversed(boundsRect,thePattern,level-1,0,x,y);
  116.                 break;
  117.             case 0x69:    /* call Y */
  118.                 if (level>1)
  119.                     HilbertFadeRecurseReversed(boundsRect,thePattern,level-1,1,x,y);
  120.                 break;
  121.         }
  122.     }
  123. }
  124.  
  125. pascal short HilbertFadeReversed(Rect boundsRect, Pattern *thePattern)
  126. {
  127.     int            curx, cury;
  128.     int            answer, i;
  129.     
  130.     answer=1;
  131.     for (i=0; i<RecursionLevel; i++)
  132.         answer*=2;
  133.     vBlockSize=1+theWindowHeight/answer;    /* used to be 20 */
  134.     hBlockSize=1+theWindowWidth/answer;        /* used to be 32 */
  135.     HilbertDirection=0;
  136.     boundsRgn=NewRgn();
  137.     SetRectRgn(boundsRgn, boundsRect.left, boundsRect.top, boundsRect.right, boundsRect.bottom);
  138.     cury=theWindowHeight;
  139.     curx=0;
  140.     HilbertFadeRecurseReversed(boundsRect,thePattern,RecursionLevel,0,&curx,&cury);
  141.     FillRect(&boundsRect, *thePattern);            /* in case we missed any bits */
  142.     
  143.     DisposeRgn(boundsRgn);
  144.     
  145.     return 0;
  146. }
  147.